package com.shabox.alopaydemo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.shabox.alopaydemo.config.AliSDK;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.Set;

@Controller()
@RequestMapping("ali")
public class AlipayController {

    @Autowired
    private AlipayClient alipayClient;

    private int index = 11;

    @RequestMapping("/index")
    public String index() {
        return "index";
    }

    @RequestMapping("/pay")
    @ResponseBody
    public JSONObject alipay() throws Exception {
//        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();//创建API对应的request类
//        request.setNotifyUrl("http://hzk.free.idcfengye.com/ali/alnotify");
//        String orderNo = "201503200101010" + index;
//        request.setBizContent("{" +
//                "    \"out_trade_no\":\"" + orderNo + "\"," +//商户订单号
//                "    \"total_amount\":\"88.88\"," +
//                "    \"subject\":\"Iphone6 16G\"," +
//                "    \"store_id\":\"NJ_001\"," +
//                "    \"timeout_express\":\"90m\"}");//订单允许的最晚付款时间
//        index++;
//        AlipayTradePrecreateResponse response = alipayClient.execute(request);
//        JSONObject jsonObj = JSONObject.parseObject(response.getBody());
//        return jsonObj;
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();//创建API对应的request
        alipayRequest.setNotifyUrl("http://hzk.free.idcfengye.com/ali/alnotify");//在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent("{" +
                "    \"out_trade_no\":\"20150320010101001\"," +
                "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
                "    \"total_amount\":88.88," +
                "    \"subject\":\"Iphone6 16G\"," +
                "    \"body\":\"Iphone6 16G\"," +
                "    \"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\"," +
                "    \"extend_params\":{" +
                "    \"sys_service_provider_id\":\"2088511833207846\"" +
                "    }"+
                "  }");//填充业务参数
        String form="";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("content", form);
        return jsonObject;
    }

    @RequestMapping("/alnotify")
    public void alnotify(@RequestParam Map<String,String> param) throws AlipayApiException {
        System.out.println(JSONObject.toJSONString(param));
        boolean signVerified = AlipaySignature.rsaCheckV1(param, AliSDK.ALIPAY_PUBLIC_KEY, AliSDK.CHARSET,AliSDK.SIGN_TYPE);//调用SDK验证签名
        if(signVerified){
            // TODO 验签成功则继续业务操作，最后在response中返回success
            System.out.println("success");
        }else{
            // TODO 验签失败则记录异常日志，并在response中返回failure.
            System.out.println("failure");
        }
    }
}
